#include "../../../common/mysql_message.hpp"

#include <gflags/gflags.h>


DEFINE_bool(run_mode,false,"程序的运行模式，false： 调试 true： 发布");
DEFINE_string(log_file,"","发布模式下，向文件中输出，如果是调试模式下，模式是stdout");
DEFINE_int32(log_level,0,"发布模式下，用于指定的日志输出等级");

DEFINE_string(host,"127.0.0.1","这是mysql的地址");
DEFINE_int32(port,0,"这是mysql的端口");
DEFINE_string(db,"lch_im","数据库默认的库");
DEFINE_string(user,"root","这是用户名");
DEFINE_string(passward,"123456","这是密码");
DEFINE_string(cset,"utf8","这是mysql客户端字符集");
DEFINE_int32(max_pool,3,"这是mysql连接池最大连接数量");

void insert_test(MessageTable& tb)
{
    Message m1("消息ID1", "会话ID1", "用户ID1", 0, boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    tb.insert(m1);
    Message m2("消息ID2", "会话ID1", "用户ID2", 0, boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    tb.insert(m2);
    Message m3("消息ID3", "会话ID1", "用户ID3", 0, boost::posix_time::time_from_string("2002-01-22 23:59:59.000"));
    tb.insert(m3);

    Message m4("消息ID4", "会话ID2", "用户ID4", 0, boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    tb.insert(m4);
    Message m5("消息ID5", "会话ID2", "用户ID5", 0, boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    tb.insert(m5);
}


void remove_test(MessageTable& tb) 
{
    tb.remove("会话ID1");
}

void recent_test(MessageTable &tb) {
    auto res = tb.recent("会话ID2", 2);
    auto begin = res.rbegin();
    auto end = res.rend();
    for (; begin != end; ++begin) {
        std::cout << begin->message_id() << std::endl;
        std::cout << begin->session_id() << std::endl;
        std::cout << begin->user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(begin->create_time()) << std::endl;
    }
}


void range_test(MessageTable &tb) {
    boost::posix_time::ptime stime(boost::posix_time::time_from_string("2002-01-20 23:59:59.000"));
    boost::posix_time::ptime etime(boost::posix_time::time_from_string("2002-01-21 23:59:59.000"));
    auto res = tb.range("会话ID1", stime, etime);
    for (const auto &m : res) {
        std::cout << m.message_id() << std::endl;
        std::cout << m.session_id() << std::endl;
        std::cout << m.user_id() << std::endl;
        std::cout << boost::posix_time::to_simple_string(m.create_time()) << std::endl;
    }
}

int main(int argc,char* argv[]) 
{
    google::ParseCommandLineFlags(&argc,&argv,true);
    init_logger(FLAGS_run_mode,FLAGS_log_file,FLAGS_log_level);

    
    auto db = ODBFactory::create(FLAGS_user,FLAGS_passward,FLAGS_db,FLAGS_host,FLAGS_cset,0,1);
    MessageTable tb(db);

    insert_test(tb);
    //remove_test(tb);
    //recent_test(tb);
    range_test(tb);

    return 0;
}